<template>
  <div class="box">
    <h2>
      角色管理页面<small
        ><el-button type="primary" size="small" round @click="showDialog()"
          >新增角色</el-button
        ></small
      >
    </h2>
    <el-table height="400" :data="roles.rolesList" width="100%">
      <el-table-column prop="id" label="序号" width="80"> </el-table-column>
      <el-table-column prop="rolename" label="角色名称" width="200">
      </el-table-column>
      <el-table-column prop="menus" label="可访问菜单"> </el-table-column>
      <el-table-column prop="status" label="角色状态">
        <template v-slot="props">
          <el-tag type="success" size="small" v-if="props.row.status === 1"
            >启用</el-tag
          >
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template v-slot="props">
          <el-button
            type="primary"
            size="small"
            round
            @click="editRole(props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            round
            @click="delRole(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Dialog ref="VDialog"></Dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
// 导入store
import useRoleStore from "../../stores/roles.js";
import { ElMessage, ElMessageBox } from "element-plus";
// 导入弹窗组件
import Dialog from "./dialog.vue";
// 声明变量
const roles = useRoleStore();
// 获取数据
onMounted(() => {
  // 调用函数,获取菜单数据
  roles.getRoleListActions();
});
// 弹窗变量
const VDialog = ref(null);
// 打开弹窗
const showDialog = () => {
  VDialog.value.dialogVisible = true;
  // console.log(VDialog.value.dialogVisible);
};

//删除角色
function delRole(Roles) {
  // console.log("roles", Roles);
  ElMessageBox.confirm("确定删除此角色吗？", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 调用删除函数
      roles.delRoleListActions(Roles.id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除此角色",
      });
    });
}
// 编辑角色
function editRole(Roles) {
  // console.log("11111111111111", VDialog.value, Roles.id);
  // 显示弹窗，将数据发送给子组件
  VDialog.value.acceptValue(Roles);
  showDialog();
}
</script>

<style lang="css" scoped></style>
